package com.automannn.springPractice.applicationContext.bean.definition;

import com.automannn.springPractice.applicationContext.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.*;

/**
 * bean定义创建演示
 *
 * @author huangk
 * @date 2022/08/03
 */
public class BeanDefinitionCreateDemo {

    public static void main(String[] args) {
        //1.通过BeanDefinitionBuilder构建
        BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        builder.addPropertyValue("name","hk");
        builder.addPropertyValue("age",11);
        AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
        // 2.通过AbstractBeanDefinition
        GenericBeanDefinition genericBeanDefinition=new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(User.class);
        MutablePropertyValues propertyValues=new MutablePropertyValues();
        propertyValues.addPropertyValue("name","huangkai123");
        propertyValues.addPropertyValue("age",33);
        propertyValues.add("name","huangkai123");
        propertyValues.add("age",33);
        genericBeanDefinition.setPropertyValues(propertyValues);

        DefaultBeanNameGenerator defaultBeanNameGenerator=new DefaultBeanNameGenerator();
        BeanDefinitionRegistry registry=new DefaultListableBeanFactory();
        String name = defaultBeanNameGenerator.generateBeanName(genericBeanDefinition, registry);
        System.out.println(name);
    }
}
